<?php

require_once APPPATH."/controllers/BasePriv.php";
require_once APPPATH."/libraries/aws/aws-autoloader.php";
require_once APPPATH."/libraries/Tools.php";
use Aws\S3\S3Client;
class Activity extends BasePriv
{
    public function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form', "url"));
        $this->load->model("Host_activity_activity_model", "table");
        $this->activity="activity";
    }

    /**
     * 活动管理下的专题活动
     */
    public function index(){
        //分页
        $arr=[];
        $arr['page']=intval($this->get('page'))>0 ? intval($this->get('page')) : 1;
        $arr['limit']=intval($this->get('limit'))>0 ? intval($this->get('limit')) : 10;
        $res=$this->table->all($arr);

        //分类
        $fenlei=$this->table->get_fenlei();
        //话题
        $huati=$this->table->get_huati("",2);
        if(!empty($res['row'])){
            foreach($res['row'] as $k=>&$v){
                foreach($fenlei as $kk=>$vv){
                    if($v['fenlei']==$vv['id']){
                        $v['fenlei_name']=$vv['name'];
                    }
                }
                foreach($huati as $kkk=>$vvv){
                    if($v['huati']==$vvv['id']){
                        $v['huati_name']=$vvv['sub_title'];
                    }
                }
            }
        }
        $data['data']=$res;
        $data['limit']=$arr['limit'];
        $data['page']=$arr['page'];
        $this->layout("activity/zhuanti_activity/index",$data);
    }

    /**
     * 新增专题
     */
    public function add_zhuanti_aactivity(){
        //获取分类
        $res=$this->table->get_fenlei();
        $data['fenlei']=$res;
        $this->layout("activity/zhuanti_activity/add_zhuanti_aactivity",$data);
    }

    /**
     * 保存新增专题
     */
    public function save_add_zhuanti_aactivity(){
        if(empty($_POST['activity_big_small'])){
            response(-1,"请选择活动类型");
        }
        if(empty($_POST['fenlei'])){
            response(-1,"请选择分类");
        }
        if(empty($_POST['huati'])){
            response(-1,"请选择话题");
        }
        if(empty($_POST['activity_pic'])){
            response(-1,"请添加活动封面");
        }
        $data['activity_big_small']=$_POST['activity_big_small'];
        $data['fenlei']=$_POST['fenlei'];
        $data['huati']=$_POST['huati'];
        $data['activity_pic']=$_POST['activity_pic'];
        $data['add_time']=date("Y-m-d H:i:s",time());
        $data['update_time']=date("Y-m-d H:i:s",time());
//        var_dump($data);exit;
        $res=$this->table->add($this->activity,$data);
        if($res){
            response(0,"添加成功",base_url("activity/activity/index"));
        }else{
            response(-1,"添加失败");
        }
    }

    /**
     * 修改专题
     */
    public function edit_add_zhuanti_aactivity(){
        if(empty($_REQUEST['id'])){
            response(-1,"缺少参数");
        }
        $id=$_REQUEST['id'];
        $res=$this->table->one($this->activity,['id'=>$id]);
        //分类
        $fenlei=$this->table->get_fenlei();
        //话题
        $huati=$this->table->get_huati($res['fenlei']);
        $data['data']=$res;
        $data['fenlei']=$fenlei;
        $data['huati']=$huati;
        $this->layout("activity/zhuanti_activity/edit_add_zhuanti_aactivity",$data);
    }

    /**
     * 保存修改专题
     */
    public function save_edit_add_zhuanti_aactivity(){
        if(empty($_POST['id'])){
            response(-1,"缺少参数");
        }
        if(empty($_POST['activity_big_small'])){
            response(-1,"请选择活动类型");
        }
        if(empty($_POST['fenlei'])){
            response(-1,"请选择分类");
        }
        if(empty($_POST['huati'])){
            response(-1,"请选择话题");
        }
        if(empty($_POST['activity_pic'])){
            response(-1,"请添加活动封面");
        }
        $data['activity_big_small']=$_POST['activity_big_small'];
        $data['fenlei']=$_POST['fenlei'];
        $data['huati']=$_POST['huati'];
        $data['activity_pic']=$_POST['activity_pic'];
        $data['update_time']=date("Y-m-d H:i:s",time());
//        var_dump($data);exit;
        $res=$this->table->update($this->activity,$data,['id'=>$_POST['id']]);
        if($res){
            response(0,"修改成功",base_url("activity/activity/index"));
        }else{
            response(-1,"修改失败");
        }
    }

    /**
     * 启用
     */
    public function activity_up(){
        if(empty($_REQUEST['id'])){
            response(-1,"缺少参数");
        }
        $id=$_REQUEST['id'];
        $data['online']=2;
        $res=$this->table->update($this->activity,$data,['id'=>$id]);
        if($res){
            response(0,"启用成功");
        }else{
            response(-1,"启用失败");
        }
    }

    /**
     * 停用
     */
    public function activity_down(){
        if(empty($_REQUEST['id'])){
            response(-1,"缺少参数");
        }
        $id=$_REQUEST['id'];
        $data['online']=1;
        $res=$this->table->update($this->activity,$data,['id'=>$id]);
        if($res){
            response(0,"停用成功");
        }else{
            response(-1,"停用失败");
        }
    }

    /**
     * 异步获取分类
     */
    public function get_fenlei(){
        $res=$this->table->get_fenlei();
        response(0,"查询成功",$res);
    }

    /**
     * 异步获取话题
     */
    public function get_huati(){
        $huati_id=$_POST['huati_id'];
        if(empty($huati_id)){
            response(-1,"获取失败，缺少参数");
        }
        $res=$this->table->get_huati($huati_id);
        response(0,"查询成功",$res);
    }

}